package task;

class MyBlockingQueue{
    private Object object = new Object();
    private int[] arr;
    private volatile int head = 0;
    private volatile int last = 0;
    private volatile int size = 0;
    public MyBlockingQueue(int n){
        arr = new int[n];
    }
    public void put(int n) throws InterruptedException {
        synchronized (object){
            if(size == arr.length){
                object.wait();
            }
            arr[last] = n;
            last++;
            if(last >= arr.length){
                last = 0;
            }
            size++;
            object.notify();
        }

    }
    public int take() throws InterruptedException {
        int n = arr[head];
        synchronized (object) {
            if (size == 0) {
                object.wait();
            }
            head++;
            if (head >= arr.length) {
                head = 0;
            }
            size--;
            object.notify();
        }
        return n;
    }
}
public class Test7 {

}
